package com.hmyd.hmyd_android.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.dto.Recommend;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.RoundImageView;

public class RecommendListAdapter extends BaseAdapter {

	private Context mContext;
	private List<Recommend> mPlist;
	private LayoutInflater mInflater;

	public RecommendListAdapter(Context context, List<Recommend> plist) {
		super();

		this.mContext = context;
		this.mPlist = plist;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return mPlist != null ? mPlist.size() : 0;
	}

	@Override
	public Recommend getItem(int position) {
		return mPlist != null ? mPlist.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (mPlist == null || mPlist.get(position) == null) {
			return convertView;
		}

		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_recommend_list_item, null);
			holder = new ViewHolder();
			holder.icon = (RoundImageView) convertView.findViewById(R.id.icon);
			holder.nick_name = (TextView) convertView.findViewById(R.id.nick_name);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.amount = (TextView) convertView.findViewById(R.id.amount);
//			holder.line = convertView.findViewById(R.id.line);
//
//			holder.frist = convertView.findViewById(R.id.frist);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Recommend recommend = getItem(position);
		if (recommend != null) {
			if (TextUtils.isEmpty(recommend.avatar)) {
				holder.icon.setImageResource(R.drawable.touxin);
			} else {
				AppBasic.getBitmapUtils(mContext).display(holder.icon, recommend.avatar);
			}

			holder.nick_name.setText(recommend.username);

			if (!TextUtils.isEmpty(recommend.realname)) {
				String realname = recommend.realname;
				String m = "";
				for (int i = 1; i < realname.length(); i++) {
					m = m + "*";
				}

				holder.nick_name.setText(recommend.username + "("
						+ (realname.subSequence(0, 1) + m) + ")");
			}

			holder.time.setText(Utils.getDateStringDay(recommend.createdTime));
			holder.amount.setText("投资次数：" + recommend.investCount);

			/*if (mPlist.size() == (position + 1)) {
				holder.line.setVisibility(View.INVISIBLE);
			} else {
				holder.line.setVisibility(View.VISIBLE);
			}*/

			/*if (position == 0) {
				holder.frist.setVisibility(View.VISIBLE);
			} else {
				holder.frist.setVisibility(View.GONE);
			}*/

		}

		return convertView;
	}

	class ViewHolder {
		public RoundImageView icon;
		public TextView nick_name;
		public TextView time;
		public TextView amount;
//		public View line;

//		public View frist;
	}

}
